<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<TwitterMatic.Shared.Domain.TimedTweet>>" %>

<asp:Content ID="title" ContentPlaceHolderID="TitleContent" runat="server">
	List of archived tweets for <%=User.Identity.Name%>
</asp:Content>

<asp:Content ID="content" ContentPlaceHolderID="MainContent" runat="server">
    <script type="text/javascript">
        $(function() {
            fixUtcDates();
        
            $(".confirmAction").each(function(i, item) {
                $(item).click(function(e) {
                    if (!confirm("Are you sure?")) {
                        e.preventDefault();
                    }
                });
            });
        });

        var fixUtcDates = function() {
            var now = new Date();
            var offset = now.getTimezoneOffset();

            $(".utcDate").each(function(i, item) {
                var parsedInt = Date.parse($(item).html());

                if (!isNaN(parsedInt)) {
                    var parsed = new Date();
                    parsed.setTime(parsedInt);
                    parsed.setMinutes(parsed.getMinutes() - offset);

                    var curr_hour = parsed.getHours();
                    if (curr_hour < 12) {
                        a_p = "am";
                    }
                    else {
                        a_p = "pm";
                    }
                    if (curr_hour == 0) {
                        curr_hour = 12;
                    }
                    if (curr_hour > 12) {
                        curr_hour = curr_hour - 12;
                    }

                    $(item).html(leadingZero(parsed.getMonth() + 1) + '/' + leadingZero(parsed.getDate()) + '/' + parsed.getFullYear() + ' ' + leadingZero(curr_hour) + ':' + leadingZero(parsed.getMinutes()) + ' ' + a_p);
                }
            });
        }

        var leadingZero = function(val) {
            if (val < 10)
                val = "0" + val;
            return val;
        }
	</script>
	
    <h3>List of archived tweets for <%=User.Identity.Name%></h3>
    
    <% Html.RenderPartial("TweetTimeline", "/Tweet/Archived"); %>
    
    <table width="100%">
        <tr>
            <th></th>
            <th>
                Message
            </th>
            <th>
                Send On
            </th>
            <th>
                Sent On
            </th>
            <th>
                Status
            </th>
        </tr>

    <% foreach (var item in Model) { %>
    
        <tr>
            <td>
                <%= Html.ActionLink("Delete", "Delete", new { id = item.RowKey }, new { @class = "confirmAction" })%>
            </td>
            <td class="comment">
                <%= Html.Encode(item.Status) %>
            </td>
            <td>
                <span class="utcDate"><%= Html.Encode(String.Format("{0:g}", item.SendOn.ToLocalTime())) %></span>
            </td>
            <td>
                <span class="utcDate">
                <% if (item.SendStatus != "Scheduled" && item.SendStatus != "Pending delivery")
                   {
                       Response.Write(Html.Encode(String.Format("{0:g}", item.SentOn.ToLocalTime())));
                   }
                   else
                   {
                       Response.Write("Not sent");
                   }
                %>
                </span>
            </td>
            <td>
                <%= Html.Encode(item.SendStatus) %>
            </td>
        </tr>
    
    <% } %>

    </table>

    <p style="clear: both;">
        <%= Html.ActionLink("Schedule new tweet", "Create", "Tweet", null, new { @class = "more" })%>
    </p>
    
    <p style="clear: both;">&nbsp;</p>
</asp:Content>

